<?php
namespace Topxia\AdminBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
use Topxia\Common\Paginator;
use Topxia\Common\ArrayToolkit;

class ActiveMaController extends BaseController
{
	public function passAction(Request $request)
	{	
		
		if($request->getMethod() == 'POST'){
			$pass = $request->request->all();
			if($pass['password'] == '123456')
			{
				return $this->redirect($this->generateUrl('admin_activema_show'));
			}else{

				$this->setFlashMessage('danger','密码错误');
				
				return $this->redirect($this->generateUrl('admin_activema_pass'));
				
			}
		}
		
		return $this->render('TopxiaAdminBundle:ActiveMa:pass.html.twig');

	}


	public function showAction(Request $request)
	{

		$conditions = $request ? $request->query->all() : array();

		$conditions['activema'] = isset($conditions['activema']) ? trim($conditions['activema']) : null;
		$conditions['mac'] = isset($conditions['mac']) ? trim($conditions['mac']) : null;
		$conditions['starttime'] = isset($conditions['starttime']) ? strtotime($conditions['starttime']) : null;
		$conditions['endtime'] = isset($conditions['endtime']) ? strtotime($conditions['endtime']) : null;
		$conditions['area'] = isset($conditions['area']) ? trim($conditions['area']) : null;

		$count = $this->getActiveMaService()->getActiveMaCount(array_filter($conditions)); 
        $paginator = new Paginator($this->get('request'), $count, 20);

		$activemas = $this->getActiveMaService()->getActiveMa(
			array_filter($conditions),
            $paginator->getOffsetCount(),
            $paginator->getPerPageCount()
		);

		foreach($activemas as $key => $val){
			if($activemas[$key]['starttime'])
				$activemas[$key]['starttime']=date('Y-m-d H:i:s',$val['starttime']);
			if($activemas[$key]['endtime'])
				$activemas[$key]['endtime']=date('Y-m-d H:i:s',$val['endtime']);
		}

		$counttype['jihuo'] = $this->getActiveMaService()->getActiveMaCount(array('status'=>1));
		$counttype['weijihuo'] = $this->getActiveMaService()->getActiveMaCount(array('status'=>0));
		$counttype['zong'] = $this->getActiveMaService()->getActiveMaCount(array());

		return $this->render("TopxiaAdminBundle:ActiveMa:index.html.twig",array(
			'activemas' => $activemas,
			'paginator' => $paginator,
			'counttype' => $counttype,
			'conditions'=> $conditions
		));


	}

	public function trylistAction(Request $request)
	{
		$conditions = $request ? $request->query->all() : array();
		$conditions['mac'] = isset($conditions['mac']) ? trim($conditions['mac']) : null;
		$conditions['starttime'] = isset($conditions['starttime']) ? strtotime($conditions['starttime']) : null;
		$conditions['endtime'] = isset($conditions['endtime']) ? strtotime($conditions['endtime']) : null;
		$conditions['area'] = isset($conditions['area']) ? trim($conditions['area']) : null;
		
		$count = $this->getActiveMaService()->gettryCount(array_filter($conditions)); 
        $paginator = new Paginator($this->get('request'), $count, 20);

		$trylist = $this->getActiveMaService()->gettrylist(array_filter($conditions),$paginator->getOffsetCount(),$paginator->getPerPageCount());

		foreach($trylist as $key => $val)
			if($trylist[$key]['starttime'])
				$trylist[$key]['starttime']=date('Y-m-d H:i:s',$val['starttime']);

		return $this->render('TopxiaAdminBundle:ActiveMa:try.html.twig',array(
			'paginator' => $paginator,
			'trylist'=>$trylist
		));
	}

	public function buildAction(Request $request)
	{
		$conditions = $request->query->all();
		$success = $this->getActiveMaService()->buildding($conditions);
		if($success == '1'){
			$this->setFlashMessage('success', '生成成功！');
		}else{
			$this->setFlashMessage('error', '生成失败');
		}
		return $this->redirect($this->generateUrl('admin_activema_show'));
	}

	public function setAction(Request $request)
	{
		$conditions = $request->query->all();
		if(isset($conditions['set']) && $conditions['set'] == '1')
			$success = $this->getActiveMaService()->set($conditions);
		elseif(isset($conditions['reset']) && $conditions['reset'] == '1')
			$success = $this->getActiveMaService()->reset($conditions);

		$this->setFlashMessage('success', '设置成功！');
		return $this->redirect($this->generateUrl('admin_activema_show'));
	}

	protected function getActiveMaService()
	{
		return $this->getServiceKernel()->createService('Kuke.ActiveMaService');
	}
}